<?php

function template($template, $id='template')
{
	if ($template == "") {$template = "defaut";}
	
	$data = "<select name=\"".$id."\" id=\"".$id."\">\n";
	$opendir = opendir("templates/");
    while (false !== ($fichier = readdir($opendir)))
	{
        if ($fichier != "." && $fichier != ".." && $fichier != "index.html") 
		{
            if ($template == $fichier){$checked = "selected";} else {$checked = "";} 
			if (is_file("templates/".$fichier."/template.php")) {$data .= "<option value=\"".$fichier ."\" ".$checked.">".$fichier."</option>\n";} 
		} 
	} 
	closedir($opendir);
	$data .= "</select>\n";
	
	return $data;
}

function load_template($nom, $defaut)
{
	global $session;
	
	ob_start();
	if(is_file("templates/".$session['6']."/tpl/".$nom)) 
	{
		require("templates/".$session['6']."/tpl/".$nom);
	}
	else 
	{
		require("templates/defaut/tpl/".$defaut);
	}
	$template = ob_get_contents();
	ob_end_clean();
		
	return $template;
}

function load_variable($var_a, $var_b, $data)
{
	$data = str_replace($var_a, $var_b, $data);
	
	return $data;
}

function load_couleur($data)
{
	global $couleur;
	
	foreach ($couleur as $key => $value) 
	{
		$data = str_replace("(CL_".$key.")", $value, $data);
	}
	
	return $data;
}

function load_style($nom)
{
	global $globales, $session, $couleur;
	
	if(is_file("templates/".$session['6']."/".$nom.".css")) 
	{
		ob_start();
		include("templates/".$session['6']."/".$nom.".css");
		$data = ob_get_contents();
		ob_end_clean();
	}
	
	$data = load_couleur($data);
	
	echo "<style type=\"text/css\">".$data."</style>";
}

function rcp_template()
{
	global $session;
	
	// Interface de la colonne centre par defaut //
	$fopen = fopen(CACHES_INTERFACES,"r");
	while (!feof($fopen)) 
	{
		$ligne = fgets($fopen, 4096);
		if($ligne != "")
		{
			$array = explode('>', $ligne); 
			$pseudo = $array['0'];
			$gauche = $array['1'];
			$centre = $array['2'];
			$droite = $array['3'];

			if($pseudo == "defaut")
			{
				$tpl['defaut']['gauche'] = $gauche;
				$tpl['defaut']['centre'] = $centre;
				$tpl['defaut']['droite'] = $droite;
			}
			
			if(($session['2'] != "visiteur")&&($pseudo == $session['2']))
			{
				$tpl['membre']['gauche'] = $gauche;
				$tpl['membre']['centre'] = $centre;
				$tpl['membre']['droite'] = $droite;
			}
		}
	}
	fclose($fopen);
	
	if (!isset($tpl['membre'])) 
	{
		$tpl_final = $tpl['defaut'];
	}
	else
	{
		$tpl_final = $tpl['membre'];
	}
	
	return $tpl_final;
}

function rcp_block($id)
{
	require(CACHES_BLOCKS);
	
	$block_info['id'] = $id;
	$block_info['activer'] = $data[$id]['activer'];
	$block_info['titre'] = $data[$id]['titre'];
	$block_info['image'] = $data[$id]['image'];
	$block_info['module'] = $data[$id]['module'];
	$block_info['block'] = $data[$id]['block'];
	$block_info['taille'] = $data[$id]['taille'];
	$block_info['text'] = $data[$id]['text'];
	$block_info['template'] = $data[$id]['template'];
	$block_info['options'] = $data[$id]['options'];
	
	return $block_info;
}

function tpl_gauche()
{
	$recup_tpl = rcp_template();
	$gauche = $recup_tpl['gauche'];

	$i = 0; 
	
	if ($gauche != "")
	{
		$array = explode('-', $gauche);
		$max = count($array); 

		while( $i < $max ) 
		{
			$block = rcp_block($array[$i]);

			if($block['activer'] == "oui")
			{
				if ($block['text'] == "")
				{
					ob_start();
					global $options;
					$options = $block['options'];
					appel_de_page($block['module'], $block['block']);
					$contenu = ob_get_contents();
					ob_end_clean();
				}
				else
				{
					$contenu = $block['text'];
					$contenu = str_replace("&lt;","<",$contenu);
					$contenu = str_replace("&gt;",">",$contenu);
				}
				
				$url_image = up_modules.$block['image'];

				$data[$i] = load_template($block['template'], "gauche.html");
				$data[$i] = load_variable("(TITRE)", $block['titre'], $data[$i]);
				$data[$i] = load_variable("(IMAGE)", $url_image, $data[$i]);
				$data[$i] = load_variable("(CONTENU)", $contenu, $data[$i]);
				$data[$i] = load_couleur($data[$i]);
				
			}
			
			$i++; 
		}

		foreach ($data as $key => $value) {echo $value;}
	}
}

function tpl_droite()
{
	$recup_tpl = rcp_template();
	$droite = $recup_tpl['droite'];

	$i = 0; 
	
	if ($droite != "")
	{
		$array = explode('-', $droite);
		$max = count($array); 

		while( $i < $max ) 
		{
			$block = rcp_block($array[$i]);

			if($block['activer'] == "oui")
			{
				if ($block['text'] == "")
				{
					ob_start();
					global $options;
					$options = $block['options'];
					appel_de_page($block['module'], $block['block']);
					$contenu = ob_get_contents();
					ob_end_clean();
				}
				else
				{
					$contenu = $block['text'];
					$contenu = str_replace("&lt;","<",$contenu);
					$contenu = str_replace("&gt;",">",$contenu);
				}
				
				$url_image = up_modules.$block['image'];

				$data[$i] = load_template($block['template'], "droite.html");
				$data[$i] = load_variable("(TITRE)", $block['titre'], $data[$i]);
				$data[$i] = load_variable("(IMAGE)", $url_image, $data[$i]);
				$data[$i] = load_variable("(CONTENU)", $contenu, $data[$i]);
				$data[$i] = load_couleur($data[$i]);
				
			}
			
			$i++; 
		}

		foreach ($data as $key => $value) {echo $value;}
	}
}

function tpl_centre()
{
	$recup_tpl = rcp_template();
	$centre = $recup_tpl['centre'];

	$i = 0; 
	
	if ($centre != "")
	{
		$array = explode('-', $centre);
		$max = count($array); 

		while( $i < $max ) 
		{
			$block = rcp_block($array[$i]);

			if($block['activer'] == "oui")
			{
				if ($block['text'] == "")
				{
					ob_start();
					global $options;
					$options = $block['options'];
					appel_de_page($block['module'], $block['block']);
					$contenu = ob_get_contents();
					ob_end_clean();
				}
				else
				{
					$contenu = $block['text'];
					$contenu = str_replace("&lt;","<",$contenu);
					$contenu = str_replace("&gt;",">",$contenu);
				}
			
				$url_image = up_modules.$block['image'];

				$data[$i] = load_template($block['template'], "centre.html");
				$data[$i] = load_variable("(TITRE)", $block['titre'], $data[$i]);
				$data[$i] = load_variable("(IMAGE)", $url_image, $data[$i]);
				$data[$i] = load_variable("(CONTENU)", $contenu, $data[$i]);
				$data[$i] = load_couleur($data[$i]);
				
			}
			
			$i++; 
		}

		foreach ($data as $key => $value) {echo $value;}
	}
}

function tpl_centre1()
{
	global $do, $fi;

	ob_start();
	appel_de_page($do, $fi);
	$contenu = ob_get_contents();
	ob_end_clean();
	
	$data = load_template("centre-1.html", "centre-1.html");
	$data = load_variable("(CONTENU)", $contenu, $data);
	$data = load_couleur($data);

	echo $data;
}

function tpl_sans() 
{
	appel_de_page("", "");
}

?>